package com.lc.websocket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import org.junit.Test;

public class PushTest {
	
	private static final String url = "ws://localhost:8080/study_push/websocket";
	private Session session;
	
	public void start() {
		WebSocketContainer container = null;
		try{
			container = ContainerProvider.getWebSocketContainer();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			URI r = URI.create(url);
			session = container.connectToServer(PushClient.class, r);
		} catch (DeploymentException | IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 客户端连接test
	 */
	@Test
	public void clientTest() {
		PushTest client = new PushTest();
		client.start();
		BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
		String input = "";
		do {
			try {
				input = bd.readLine();
				session.getBasicRemote().sendText(input);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} while (!"exit".equals(input));
	}
	
}
